﻿using octopus_project.screen;
using System.Collections.Generic;
using octopus_project.tileset;
using Microsoft.Xna.Framework;
using octopus_project.scene.cannon.weapon;
using octopus_project.input;
using System;
using octopus_project.scene.war;
using octopus_project.typegame;
using octopus_project.tileset.mapCells;

namespace octopus_project.scene.cannon {
    class Cannon_Gameplay : Gameplay_GameScreen {
        Dictionary<PlayerIndex, Cannon_Cursor> cursors;
        Dictionary<PlayerIndex, StatusBar_GameplayEntry> status;

        //private Timer_GameplayEntry timer;
        private TypeGame typeGame;

        /// <summary>
        /// Description.
        /// </summary>
        public Cannon_Gameplay(ScreenManager screenManager, TypeGame paramTypeWar)
            : base(screenManager) {

            typeGame = paramTypeWar;
            TransitionOffTime = TimeSpan.Zero;           
        }

        public void createAll() {

            cursors = new Dictionary<PlayerIndex, Cannon_Cursor>();
            status = new Dictionary<PlayerIndex, StatusBar_GameplayEntry>();

            Cannon_Cursor cursor1 = new Cannon_Cursor(this, Game1.Players[PlayerIndex.One]);
            cursor1.PositionX = 15;
            cursor1.PositionY = 13;
            cursor1.CurrentWeapon = new MiniCannon();
            cursors.Add(PlayerIndex.One, cursor1);
            status.Add(PlayerIndex.One, new StatusBar_GameplayEntry(this, Game1.Players[PlayerIndex.One]));

            Cannon_Cursor cursor2 = new Cannon_Cursor(this, Game1.Players[PlayerIndex.Two]);
            cursor2.PositionX = 89;
            cursor2.PositionY = 13;
            cursor2.CurrentWeapon = new MiniCannon();
            cursors.Add(PlayerIndex.Two, cursor2);
            status.Add(PlayerIndex.Two, new StatusBar_GameplayEntry(this, Game1.Players[PlayerIndex.Two]));


           /* Cannon_Cursor cursor3 = new Cannon_Cursor(this, Game1.Players[PlayerIndex.Three]);
            cursor3.PositionX = 15;
            cursor3.PositionY = 42;
            cursor3.CurrentWeapon = new MiniCannon();
            cursors.Add(PlayerIndex.Three, cursor3);
            status.Add(PlayerIndex.Three, new StatusBar_GameplayEntry(this, Game1.Players[PlayerIndex.Three]));

            Cannon_Cursor cursor4 = new Cannon_Cursor(this, Game1.Players[PlayerIndex.Four]);
            cursor4.PositionX = 89;
            cursor4.PositionY = 43;
            cursor4.CurrentWeapon = new MiniCannon();
            cursors.Add(PlayerIndex.Four, cursor4);
            status.Add(PlayerIndex.Four, new StatusBar_GameplayEntry(this, Game1.Players[PlayerIndex.Four]));
            */

            //timer = new Timer_GameplayEntry(this, new Vector2(748, 428), 41);

            //GameplayEntries.Add(inputHandler);
            GameplayEntries.Add(Game1.battlegroundMap);
            GameplayEntries.Add(typeGame.Timer);

            GameplayEntries.AddRange(status.Values);
            GameplayEntries.AddRange(cursors.Values);

        }



        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen) {

            if (!typeGame.Timer.isComplete) {
                base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            }
            else {
                typeGame.nextPhase();
            }

        }

        

        /// <summary>
        /// Lets the game respond to player input. Unlike the Update method,
        /// this will only be called when the gameplay screen is active.
        /// </summary>
        public override void HandleInput(InputHandler input) {
            base.HandleInput(input);

            if(input.IsNewKeyPress(KeyMap.getGeneralActionKey(ActionKey.SKIP_SCENE))) {
                typeGame.nextPhase();
            }

            foreach (Player aPlayer in Game1.Players.Values) {
                if (input.IsNewKeyPress(aPlayer.getActionKey(ActionKey.DOWN)) || input.IsNewButtonPress(aPlayer.getActionButton(ActionKey.DOWN))) {
                    if (cursors[aPlayer.PlayerIndex].PositionY < (Constant.TOTAL_TILES_Y - 1)) {
                        cursors[aPlayer.PlayerIndex].PositionY++;
                    }
                }
                if (input.IsNewKeyPress(aPlayer.getActionKey(ActionKey.UP)) || input.IsNewButtonPress(aPlayer.getActionButton(ActionKey.UP))) {
                    if (cursors[aPlayer.PlayerIndex].PositionY > 0) {
                        cursors[aPlayer.PlayerIndex].PositionY--;
                    }
                }
                if (input.IsNewKeyPress(aPlayer.getActionKey(ActionKey.RIGHT)) || input.IsNewButtonPress(aPlayer.getActionButton(ActionKey.RIGHT))) {
                    if (cursors[aPlayer.PlayerIndex].PositionX < (Constant.TOTAL_TILES_X - 1)) {
                        cursors[aPlayer.PlayerIndex].PositionX++;
                    }
                }
                if (input.IsNewKeyPress(aPlayer.getActionKey(ActionKey.LEFT)) || input.IsNewButtonPress(aPlayer.getActionButton(ActionKey.LEFT))) {
                    if (cursors[aPlayer.PlayerIndex].PositionX > 0) {
                        cursors[aPlayer.PlayerIndex].PositionX--;
                    }
                }
                if (input.IsNewKeyPress(aPlayer.getActionKey(ActionKey.FIRE)) || input.IsNewButtonPress(aPlayer.getActionButton(ActionKey.FIRE))) {
                    if (cursors[aPlayer.PlayerIndex].CurrentWeapon != null) {
                        
                        //bool wasUpdated = Game1.battlegroundMap.putCannon(aPlayer, cursors[aPlayer.PlayerIndex].PositionX, cursors[aPlayer.PlayerIndex].PositionY);
                        bool wasUpdated = MapCell.createCellCannon((byte)cursors[aPlayer.PlayerIndex].PositionX, (byte)cursors[aPlayer.PlayerIndex].PositionY, aPlayer, cursors[aPlayer.PlayerIndex].CurrentWeapon);
                            //MediumCannon_MapCell.createCannon((byte)cursors[aPlayer.PlayerIndex].PositionX, (byte)cursors[aPlayer.PlayerIndex].PositionY, aPlayer);


                        if (wasUpdated) {
                            aPlayer.Stats.WeaponList.Add(cursors[aPlayer.PlayerIndex].CurrentWeapon);
                            cursors[aPlayer.PlayerIndex].CurrentWeapon = aPlayer.Stats.nextWeapon();
                        }
                    }
                    
                }
                /*if (input.IsNewKeyPress(aPlayer.getActionKey(ActionKey.ROTATE)) || input.IsNewButtonPress(aPlayer.getActionButton(ActionKey.ROTATE))) {
                    
                }*/
            }

        }


        
        
    }
}
